package com.ctyun.xstore.vod.common;

import java.io.IOException;
import java.net.URL;

import com.ctyun.xstore.vod.auth.SignParam;
import com.ctyun.xstore.vod.auth.Signature;

import okhttp3.Request;

public abstract class AbstractDeleteRequest extends AbstractGeneralRequest {

	@Override
	protected String buildRequestUrl(String host) {
		StringBuilder requestUrl = new StringBuilder();
		requestUrl.append(host).append(this.getPath()).append("?").append(RequestUtil.getQueryString(this));
		return requestUrl.toString();
	}

	@Override
	public Request buildRequest(String host, String accessKey, String secretKey) throws IOException {
		URL url = new URL(this.buildRequestUrl(host));

		String zoneDateTime = Signature.getZoneDateTime();
		SignParam signParam = new SignParam.Builder().setAccessKey(accessKey).setSecretKey(secretKey)
				.setUri(url.getPath()).setHttpMethod("DELETE").setZoneDateTime(zoneDateTime)
				.setParameterNames(RequestUtil.getQueryParameter(this)).build();
		String signature = Signature.sign(signParam);

		Request.Builder requestBuilder = new Request.Builder().url(url).addHeader("Date", signParam.getZoneDateTime())
				.addHeader("signature", signature);

		return requestBuilder.delete().build();
	}
}
